package com.example.controller;

import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.example.config.Result;
import com.example.pojo.MsslyyAddressMessaget;
import com.example.service.MsslyyOrderDetailService;
import com.example.service.UserService;
import com.example.pojo.MsslyyOrderMaster;
import com.example.service.MsslyyOrderMasterService;
import com.example.pojo.User;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.example.utils.PageUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/msslyyOrderMaster")
@Api(tags = "订单主表")
public class MsslyyOrderMasterController {
    @Resource
    private MsslyyOrderMasterService msslyyOrderMasterService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private UserService userService;
    @Resource
    private MsslyyOrderDetailService msslyyOrderDetailService;
    @Resource
    private PageUtils pageUtils;

    public User getUser() {
        String token = request.getHeader("token");
        String username = JWT.decode(token).getAudience().get(0);
        return userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
    }
    //添加订单
    @PostMapping("/add/{userId}/{orderMasterMoney}/{bookIds}/{bookAmounts}")
    public Result<?> addOrder(@PathVariable Long userId,
                              @RequestBody MsslyyAddressMessaget address,
                              @PathVariable BigDecimal orderMasterMoney,
                              @PathVariable String bookIds,
                              @PathVariable String bookAmounts) {
        //添加订单主表
        MsslyyOrderMaster msslyyOrderMaster = msslyyOrderMasterService.addOrder(userId, address, orderMasterMoney);
        //添加订单详情表
        msslyyOrderDetailService.addOrder(msslyyOrderMaster.getId(),bookIds,bookAmounts);
        return Result.success();
    }

    @ApiOperation(value = "新增订单",notes = "")
    //根据用户拿到历史订单信息
    @GetMapping("/getHistoryOrder/{userId}")
    public Result<?> getHistoryOrder(@PathVariable Long userId,
                                     @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        List<MsslyyOrderMaster> list = msslyyOrderMasterService.historyOrder(userId);
        Page pages = pageUtils.getPages(pageNum, pageSize, list);
        return Result.success(pages);
    }
    //确认收货
    @GetMapping("/confirmReceipt/{id}")
    public Result<?> confirmReceipt(@PathVariable Long id) {
        return Result.success(msslyyOrderMasterService.confirmReceipt(id));
    }
    @PostMapping
    public Result<?> save(@RequestBody MsslyyOrderMaster msslyyOrderMaster) {
        msslyyOrderMasterService.save(msslyyOrderMaster);
        return Result.success();
    }

    @ApiOperation(value = "更新订单",notes = "")
    @PutMapping
    public Result<?> update(@RequestBody MsslyyOrderMaster msslyyOrderMaster) {
        msslyyOrderMasterService.updateById(msslyyOrderMaster);
        return Result.success();
    }

    @ApiOperation(value = "删除订单",notes = "")
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        msslyyOrderMasterService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "根据id查询",notes = "")
    @GetMapping("/{id}")
    public Result<?> findById(@PathVariable Long id) {
        return Result.success(msslyyOrderMasterService.getById(id));
    }

    @ApiOperation(value = "查询全部",notes = "")
    @GetMapping
    public Result<?> findAll() {
        List<MsslyyOrderMaster> list = msslyyOrderMasterService.list();
        return Result.success(list);
    }

    @ApiOperation(value = "分页",notes = "")
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                                @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<MsslyyOrderMaster> query = Wrappers.<MsslyyOrderMaster>lambdaQuery().orderByDesc(MsslyyOrderMaster::getId);
        if (StrUtil.isNotBlank(name)) {
            query.like(MsslyyOrderMaster::getUserId, name);
        }
        IPage<MsslyyOrderMaster> page = msslyyOrderMasterService.page(new Page<>(pageNum, pageSize), query);
        return Result.success(page);
    }

}
